using System;
using System.Data;
using System.Configuration;
using System.Xml;
using System.Web;
using umbraco.cms.businesslogic.web;
using System.Text;
using System.Globalization;
using umbraco;

namespace Nibble.AlphabetFolder
{
    public class AlphabetFolder_ActionHandler : umbraco.BusinessLogic.Actions.IActionHandler
    {
        string umbraco.BusinessLogic.Actions.IActionHandler.HandlerName()
        {
            return "Nibble.AlphabetFolder";
        }

        umbraco.interfaces.IAction[] umbraco.BusinessLogic.Actions.IActionHandler.ReturnActions()
        {
            return new umbraco.interfaces.IAction[] { new umbraco.BusinessLogic.Actions.ActionNew() };
        }

        Boolean umbraco.BusinessLogic.Actions.IActionHandler.Execute(umbraco.cms.businesslogic.web.Document documentObject, umbraco.interfaces.IAction action)
        {
            if (!this.isValid(documentObject.ContentType.Alias))
            {
                return false;
            }

            if (new Document(documentObject.Parent.Id).ContentType.Alias != "AlphabetFolder")
            {
                char firstChar = Convert.ToChar(RemoveDiacritics(documentObject.Text).Substring(0, 1).ToLower());

                string folderName = "-";

                if(Char.IsLetter(firstChar))
                {
                    folderName = firstChar.ToString();
                }

                Document document = null;
                foreach (Document aFolder in new Document(documentObject.Parent.Id).Children)
                {
                    if (aFolder.Text == folderName)
                    {
                        document = new Document(aFolder.Id);
                        break;
                    }
                }
                if (document == null)
                {
                    document = Document.MakeNew(folderName, DocumentType.GetByAlias("AlphabetFolder"), documentObject.User, documentObject.Parent.Id);
                    document.Publish(documentObject.User);
                    library.PublishSingleNode(document.Id);
                }

                documentObject.Move(document.Id);

            }


            return true;


        }

        private string RemoveDiacritics(String s)
        {
            String normalizedString = s.Normalize(NormalizationForm.FormD);
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < normalizedString.Length; i++)
            {
                Char c = normalizedString[i];
                if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
                    stringBuilder.Append(c);
            }

            return stringBuilder.ToString();
        } 


        private bool isValid(string Alias)
        {
            XmlDocument document = new XmlDocument();
            document.Load(HttpContext.Current.Server.MapPath("/config/Nibble.AlphabetFolder/AlphabetFolder.config"));
            foreach (XmlNode node in document.SelectNodes("AlphabetFolder/DocumentTypeAlias"))
            {
                if (node.InnerText.Trim() == Alias)
                {
                    return true;
                }
            }
            return false;
        }

    }
}
